package com.example.mobile2021606137xzt0618;

import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class UsersRViewAdapter extends RecyclerView.Adapter<UsersRViewAdapter.ViewHolder> {
    private List<Users> list;

    public UsersRViewAdapter(List<Users> list) {this.list = list;}

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvItemId,tvItemName,tvItemAge,tvItemScore;
        Button btnItemUpdata,btnItemDelete;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvItemId = itemView.findViewById(R.id.tvItemId);
            tvItemName = itemView.findViewById(R.id.tvItemName);
            tvItemAge = itemView.findViewById(R.id.tvItemAge);
            tvItemScore = itemView.findViewById(R.id.tvItemScore);
            btnItemUpdata = itemView.findViewById(R.id.btnItemUpdata);
            btnItemDelete = itemView.findViewById(R.id.btnItemDelete);
        }
    }
    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_main_user,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Users users = list.get(position);
        holder.tvItemId.setText(String.valueOf(users.get_id()));
        holder.tvItemName.setText(users.getName());
        holder.tvItemAge.setText(String.valueOf(users.getAge()));
        holder.tvItemScore.setText(String.valueOf(users.getScore()));
        holder.btnItemDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyDataAdapter().delete(v.getContext(),list.get(position).get_id());

                list.clear();
                list.addAll(new MyDataAdapter().QueryAll(v.getContext()));
                notifyDataSetChanged();
            }
        });

        holder.btnItemUpdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View view = LayoutInflater.from(v.getContext()).inflate(R.layout.dialog_instert,null);
                final EditText edtTxDialogName = view.findViewById(R.id.edtTextDialogName);
                final EditText edtTxDialogAge = view.findViewById(R.id.edtTextDialogAge);
                final EditText edtTxDialogScore = view.findViewById(R.id.edtTextDialogScore);

                edtTxDialogName.setText(list.get(position).getName());
                edtTxDialogAge.setText(String.valueOf(list.get(position).getAge()));
                edtTxDialogScore.setText(String.valueOf(list.get(position).getScore()));

                new AlertDialog.Builder(v.getContext()).setTitle("更新数据").setView(view).setPositiveButton("更新", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String name = edtTxDialogName.getText().toString().trim();
                        int age = Integer.valueOf(edtTxDialogAge.getText().toString().trim());
                        float score = Float.valueOf(edtTxDialogScore.getText().toString().trim());

                        new MyDataAdapter().updata(v.getContext(),list.get(position).get_id(),new Users(name,age,score));

                        list.clear();
                        list.addAll(new MyDataAdapter().QueryAll(v.getContext()));
                        notifyDataSetChanged();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create().show();
            }

        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


}
